{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%matplotlib inline \n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ejercicio: Paseo aleatorio\n",
    "En el paseo aleatorio, se parte de un valor inicial $V_0$. Se lanza una moneda equilibrada. Si sale cara, sumamos 1 al valor ($V_1 = V_0 + 1$). Si sale cruz, se lo restamos. Este proceso se repite hasta que se alcanza alguno de los valores del conjunto $B$ (bordes).\n",
    "\n",
    "Lo que se pide es implementar la funcion `caminoAleatorio(v0, bordes)`, que devuelva la lista de valores hasta llegar a un borde."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import random\n",
    "def caminoAleatorio(v0, bordes):\n",
    "    recorrido = [v0]\n",
    "    while v0 not in bordes:\n",
    "        v0 += random.choice([-1, 1])\n",
    "        recorrido.append(v0)\n",
    "    return recorrido"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]\n"
     ]
    }
   ],
   "source": [
    "#importamos el modulo random\n",
    "import random\n",
    "#definimos la función\n",
    "def caminoAleatorio(v0, bordes):\n",
    "    recorrido = [v0]#igualamos recorrido al v0 para después ir sumándoles el resultado\n",
    "    while v0 not in bordes:\n",
    "        v0 += random.choice([1,1])#mientras v0 no esté en bordes, le sumamos o restamos uno aleatoriamente\n",
    "        recorrido.append(v0) #le vamos metiendo v0 siempre que no haya salido un borde\n",
    "    return recorrido\n",
    "bordes = [-100, 100]\n",
    "camino = caminoAleatorio(8, bordes)\n",
    "print(camino)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Ahora que la tenemos, obtenemos un camino para $v_0=3$ y $B=\\{0, 10\\}$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[8, 7, 8, 9, 8, 9, 8, 9, 10, 11, 12, 11, 10, 11, 12, 13, 14, 15, 14, 13, 14, 13, 14, 13, 14, 15, 14, 13, 12, 11, 10, 9, 8, 9, 8, 9, 8, 9, 8, 7, 6, 5, 6, 7, 6, 7, 6, 5, 6, 5, 4, 5, 6, 7, 6, 7, 6, 7, 8, 7, 6, 7, 6, 7, 6, 7, 6, 7, 8, 7, 6, 7, 6, 5, 4, 3, 4, 3, 2, 1, 2, 1, 0]\n"
     ]
    }
   ],
   "source": [
    "# Ejemplo de ejecucion\n",
    "bordes = [0, 20]\n",
    "camino = caminoAleatorio(8, bordes)\n",
    "print(camino)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'plt' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-5-e6f1705cad1c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mfig\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0max\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mplt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msubplots\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0max\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mplot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcamino\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0max\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mset_xlabel\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Pasos'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0max\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mset_ylabel\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Nivel'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0max\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mset_title\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Evolución de un camino aleatorio'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m;\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'plt' is not defined"
     ]
    }
   ],
   "source": [
    "fig, ax = plt.subplots(1, 1)\n",
    "ax.plot(camino)\n",
    "ax.set_xlabel('Pasos')\n",
    "ax.set_ylabel('Nivel')\n",
    "ax.set_title('Evolución de un camino aleatorio');\n",
    "for bor in bordes:\n",
    "    ax.plot([0, len(camino)-1], [bor, bor], color='red', lw=1, linestyle='dashdot', alpha=0.6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
